<!--
 * @Author: jiangyg email@jiangyg.com
 * @Date: 2022-08-14 15:31:12
 * @LastEditors: jiangyg email@jiangyg.com
 * @LastEditTime: 2022-11-19 16:09:01
 * @FilePath: /frontend/mall-frontend/src/views/modules/admin/ware/wareinfo.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="mod-wareinfo">
    <data-table ref="dataTableRef" :criterias="table.criterias" name="wareinfo" :initLoad="true" url="/ware/wareinfo/select">
      <!-- ----------------------------------------- 查询条件 ----------------------------------------- -->
      <template v-slot:search="criterias">
        <dt-search-item label="检索内容" prop="condition">
          <el-input v-model="criterias.condition" clearable
                    maxlength="50" placeholder="请输入仓库名/地址/区域编码" style="width: 300px;"></el-input>
        </dt-search-item>
      </template>
      <!-- ------------------------------------------ 工具栏 ------------------------------------------ -->
      <template v-slot:toolbar>
        <el-button-group>
          <el-button v-if="admin.isAuth('ware:wareinfo:save')" @click="() => addDataEvent()" type="primary">
            新增
          </el-button>
          <el-button v-if="admin.isAuth('ware:wareinfo:save')" @click="() => updateDataEvent()" type="warning">
            修改
          </el-button>
          <el-button v-if="admin.isAuth('ware:wareinfo:delete')" @click="() => deleteDataEvent()" type="danger">
            删除
          </el-button>
        </el-button-group>
      </template>
      <!-- ---------------------------------------- 数据表格列 ---------------------------------------- -->
      <el-table-column fixed prop="name" show-overflow-tooltip align="left" label="仓库名" width="260" />
      <el-table-column prop="address" show-overflow-tooltip align="left" label="仓库地址" />
      <el-table-column prop="areacode" align="center" label="区域编码" width="160" />
    </data-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdateRef" @loadTableData="loadTableData"></add-or-update>
  </div>
</template>
<script lang="ts" setup>
import admin from "@/utils/admin";
import { ref, nextTick, reactive } from "vue";

import AddOrUpdate from './wareinfo-add-or-update.vue';

// 数据表格
const table = reactive({
  criterias: {
    condition: ''
  }
});

// 数据表格引用
const dataTableRef = ref();

// 弹窗引用
const addOrUpdateRef = ref();

// 新增修改弹窗是否显示
const addOrUpdateVisible = ref(false);

// 新增
const addDataEvent = async () => {
  addOrUpdateVisible.value = true
  await nextTick();
  addOrUpdateRef.value.initForm('add')
};

// 修改
const updateDataEvent = async () => {
  // 验证并获取是否选择一条数据
  let data = admin.obtainOneSelectionTableData(dataTableRef.value);
  if (!data) {
    return
  }
  addOrUpdateVisible.value = true;
  await nextTick();
  addOrUpdateRef.value.initForm('update', data)
};

// 删除
const deleteDataEvent = () => {
  admin.batchDeleteSelectionTableData(dataTableRef.value, '/ware/wareinfo/delete');
};

// 表格数据加载
const loadTableData = () => {
  dataTableRef.value.loadTableData();
};
</script>
